package com.aiju.cash.ui.stockupdate.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.StockApi;
import com.aiju.cash.ui.stockupdate.adapter.StockAdapter;
import com.aiju.cash.ui.stockupdate.bean.Stock;
import com.aiju.common.event.EventBusHandler;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.StringUtil;
import com.aiju.utils.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 流冥
 * @date: 2017-7-30
 * @Description: 库存清单首页
 */
public class StockListHomePageActivity extends BaseTitleActivity implements StockAdapter.StockListItemClickListener {
    private View empty;
    private RecyclerView recyclerviewTeams;
    private LinearLayoutManager mTeamsLayoutManager;
    private StockAdapter adapter;
    StockApi stockApi;
    private LinearLayout fake_layout;
    List<Stock.GoodsListBean> dateList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_list_home_page);
        initView();
    }

    void initView() {
        setCenterTitle("库存清单");
        empty = findViewById(R.id.iv_empty);
        fake_layout = (LinearLayout) findViewById(R.id.fake_layout);
        recyclerviewTeams = (RecyclerView) findViewById(R.id.recyclerview_member);
        mTeamsLayoutManager = new LinearLayoutManager(this);
        recyclerviewTeams.setLayoutManager(mTeamsLayoutManager);
        stockApi = new StockApi(mDialogHelper);
        getList();
        fake_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StockListHomePageActivity.this, GoodsSearchActivity.class);
                startActivity(intent);
            }
        });

        EventCenter.bindContainerAndHandler(getLifecycle(), new EventBusHandler() {
            public void onEventMainThread(Stock.GoodsListBean event) {
                notiyList(event);
            }
        });


    }

    public void getList() {
        stockApi.getShopList(new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                if (jsonData.isSuccess()) {
                    if (StringUtil.isBlank(data) || "[]".equals(data.toString())) {
                        empty.setVisibility(View.VISIBLE);
                        recyclerviewTeams.setVisibility(View.GONE);
                        return;
                    }
                    List<Stock> list = new Gson().fromJson(data, new TypeToken<List<Stock>>() {
                    }.getType());
                    if (list.size() == 0 || (list.size() == 1 && list.get(0).getGoodsList().size() == 0)) {
                        recyclerviewTeams.setVisibility(View.GONE);
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        recyclerviewTeams.setVisibility(View.VISIBLE);
                        empty.setVisibility(View.GONE);
                    }
                    dateList = setStockList(trans(list));
                    showList(dateList);
                } else {
                    T.ToastShort(jsonData.getMsg());
                }

            }
        });
    }


    void showList(List<Stock.GoodsListBean> list) {

        if (adapter == null) {
            adapter = new StockAdapter(list);
            recyclerviewTeams.setAdapter(adapter);
            final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(adapter);
            recyclerviewTeams.addItemDecoration(headersDecor);
            adapter.setListener(this);
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onItemClickListener(Stock.GoodsListBean goodsListBean) {
        Intent intent = new Intent(this, StockInfoActivity.class);
        intent.putExtra("goodsListBean", goodsListBean);
        startActivity(intent);
    }


    public List<Stock.GoodsListBean> setStockList(List<Stock> d) {
        List<Stock.GoodsListBean> list = new ArrayList<>();
        for (Stock b : d) {
            list.addAll(b.getGoodsList());
        }
        return list;
    }


    public void notiyList(Stock.GoodsListBean bean) {
        for (int i = 0; i < dateList.size(); i++) {
            if (dateList.get(i).getId() == bean.getId()) {
                dateList.set(i, bean);
                adapter.notifyDataSetChanged();
                break;
            }
        }

    }


    private List<Stock> trans(List<Stock> list) {
        for (int i = 0; i < list.size(); i++) {
            List<Stock.GoodsListBean> goodsList = list.get(i).getGoodsList();
            for (int j = 0; j < goodsList.size(); j++) {
                goodsList.get(j).setGoodsTypeName(list.get(i).getName());
            }
        }
        return list;
    }

}
